{% extends 'base.html' %}
{% block content %}
<h1>图书列表</h1>
<form method="get" class="row g-3 mb-3">
    <div class="col-auto">
        <input type="text" name="q" class="form-control" placeholder="搜索书名" value="{{ q }}">
    </div>
    <div class="col-auto">
        <button type="submit" class="btn btn-primary">搜索</button>
    </div>
</form>
<a href="{% url 'book-add' %}" class="btn btn-success mb-3">添加新图书</a>
<table class="table table-bordered table-hover bg-white align-middle">
    <thead class="table-light">
        <tr>
            <th>封面</th>
            <th>书名</th>
            <th>作者</th>
            <th>出版日期</th>
            <th>ISBN</th>
            <th>操作</th>
        </tr>
    </thead>
    <tbody>
    {% for book in books %}
        <tr>
            <td style="width:80px;">
                {% if book.cover %}
                    <img src="{{ book.cover.url }}" alt="封面" style="max-width:60px;max-height:80px;">
                {% else %}
                    <span class="text-muted">无</span>
                {% endif %}
            </td>
            <td><a href="{% url 'book-detail' book.pk %}">{{ book.title }}</a></td>
            <td>{{ book.author.name }}</td>
            <td>{{ book.published_date }}</td>
            <td>{{ book.isbn }}</td>
            <td>
                <a href="{% url 'book-edit' book.pk %}" class="btn btn-sm btn-info">编辑</a>
                <a href="{% url 'book-delete' book.pk %}" class="btn btn-sm btn-danger">删除</a>
            </td>
        </tr>
    {% empty %}
        <tr><td colspan="6">暂无数据</td></tr>
    {% endfor %}
    </tbody>
</table>
<nav>
    <ul class="pagination">
        {% if books.has_previous %}
            <li class="page-item"><a class="page-link" href="?q={{ q }}&page=1">首页</a></li>
            <li class="page-item"><a class="page-link" href="?q={{ q }}&page={{ books.previous_page_number }}">上一页</a></li>
        {% else %}
            <li class="page-item disabled"><span class="page-link">首页</span></li>
            <li class="page-item disabled"><span class="page-link">上一页</span></li>
        {% endif %}
        <li class="page-item active"><span class="page-link">{{ books.number }}/{{ books.paginator.num_pages }}</span></li>
        {% if books.has_next %}
            <li class="page-item"><a class="page-link" href="?q={{ q }}&page={{ books.next_page_number }}">下一页</a></li>
            <li class="page-item"><a class="page-link" href="?q={{ q }}&page={{ books.paginator.num_pages }}">末页</a></li>
        {% else %}
            <li class="page-item disabled"><span class="page-link">下一页</span></li>
            <li class="page-item disabled"><span class="page-link">末页</span></li>
        {% endif %}
    </ul>
</nav>
<a href="{% url 'author-list' %}" class="btn btn-secondary mt-3">作者管理</a>
{% endblock %} 